﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VerifyArgs;
using Corvus.Workflow;

namespace Corvus.Services
{
    public class PageService
    {
        private ILogger _logger;
        private IEventBroker _broker;
        private Func<IPage, IUser, SavedEventArgs<IPage>> _savedEventArgsFactory;
        private Func<IPage, IUser, SavingEventArgs<IPage>> _savingEventArgsFactory;


        public PageService(ILogger logger
            , IEventBroker broker
            , Func<IPage, IUser, SavedEventArgs<IPage>> savedEventArgsFactory
            , Func<IPage, IUser, SavingEventArgs<IPage>> savingEventArgsFactory)
        {
            Verify.Args(new { logger, broker, savedEventArgsFactory, savingEventArgsFactory }).NotNull();
            _logger = logger;
            _broker = broker;
            _savedEventArgsFactory = savedEventArgsFactory;
            _savingEventArgsFactory = savingEventArgsFactory;
        }

        public IPage FindPageByUrl(string url)
        {
            throw new NotSupportedException();
        }

        public IPage GetPageById(string id)
        {
            throw new NotSupportedException();
        }

        public void Save(IUser currentUser, IPage page)
        {
            Verify.Args(new { currentUser, page }).NotNull();
            _broker.Publish<SavingEventArgs<IPage>>(this, _savingEventArgsFactory.Invoke(page, currentUser));

            // perform saving magic

            _broker.Publish<SavedEventArgs<IPage>>(this, _savedEventArgsFactory.Invoke(page, currentUser));
        }

        public void Publish(IUser currentUser, IPage page)
        {
            Verify.Args(new { currentUser, page }).NotNull();
            throw new NotSupportedException();
        }
    }
}
